package com.example.weixintest;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    // tab切换要用到的控件
    LinearLayout tabChat,tabFriend,tabContact,tabSetting;
    ImageButton imgChat,imgFriend,imgContact,imgSetting;
    Fragment chatFragment = new ChatFragment();
    Fragment friendFragment = new FriendFragment();
    Fragment contactFragment = new ContactFragment();
    Fragment settingFragment = new SettingFragment();

    // FragmentManager是Fragment进行通讯的通讯控制器
    FragmentManager fragmentManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_main);

        // 初始化LinearLayout和ImageButton
        initView();
        // 设置控件的监听函数
        initEvent();
        // 初始化Fragment
        initFragment();
        selectTab(1);
    }

    // 监听响应函数
    @Override
    public void onClick(View v) {
        resetImg();
        switch (v.getId()){
            case R.id.linear_chat:
                Log.v("wsm","第一个tab被点击");
                selectTab(1);
                break;
            case R.id.linear_friend:
                Log.v("wsm","第二个tab被点击");
                selectTab(2);
                break;
            case R.id.linear_contact:
                Log.v("wsm","第三个tab被点击");
                selectTab(3);
                break;
            case R.id.linear_setting:
                Log.v("wsm","第四个tab被点击");
                selectTab(4);
                break;
        }
    }

    // 还原初始图标函数
    private void resetImg() {
        imgChat.setImageResource(R.drawable.tab_weixin_normal);
        imgFriend.setImageResource(R.drawable.tab_find_frd_normal);
        imgContact.setImageResource(R.drawable.tab_address_normal);
        imgSetting.setImageResource(R.drawable.tab_settings_normal);
    }

    // Fragment初始化函数
    public void initFragment(){
        fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        // 要把四个fragment放到id为content的FrameLayout中去
        transaction.add(R.id.content,chatFragment);
        transaction.add(R.id.content,friendFragment);
        transaction.add(R.id.content,contactFragment);
        transaction.add(R.id.content,settingFragment);
        transaction.commit();
    }

    // 设置监听
    private void initEvent() {
        tabChat.setOnClickListener(this);
        tabFriend.setOnClickListener(this);
        tabContact.setOnClickListener(this);
        tabSetting.setOnClickListener(this);
    }

    // 找到所有的控件
    private void initView(){
        tabChat = (LinearLayout)findViewById(R.id.linear_chat);
        tabFriend = (LinearLayout)findViewById(R.id.linear_friend);
        tabContact = (LinearLayout)findViewById(R.id.linear_contact);
        tabSetting = (LinearLayout)findViewById(R.id.linear_setting);

        imgChat = (ImageButton)findViewById(R.id.img_chat);
        imgFriend = (ImageButton)findViewById(R.id.img_friend);
        imgContact = (ImageButton)findViewById(R.id.img_contact);
        imgSetting = (ImageButton)findViewById(R.id.img_setting);
    }

    // 利用函数控制页面切换
    private void selectTab(int i){
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        hideFragment(transaction); // 将全部界面隐藏
        switch (i){
            case 1:
                transaction.show(chatFragment);
                // 设置图片为绿色的（表示选中）
                imgChat.setImageResource(R.drawable.tab_weixin_pressed);
                break;
            case 2:
                transaction.show(friendFragment);
                imgFriend.setImageResource(R.drawable.tab_find_frd_pressed);
                break;
            case 3:
                transaction.show(contactFragment);
                imgContact.setImageResource(R.drawable.tab_address_pressed);
                break;
            case 4:
                transaction.show(settingFragment);
                imgSetting.setImageResource(R.drawable.tab_settings_pressed);
                break;
            default:
                break;
        }
        transaction.commit();
    }

    // 先隐藏所有的fragment
    private void hideFragment(FragmentTransaction transaction){
        transaction.hide(chatFragment);
        transaction.hide(friendFragment);
        transaction.hide(contactFragment);
        transaction.hide(settingFragment);
    }
}